package main

import (
	"com.jtthink/go_rabbitMQ/lib"
	"com.jtthink/go_rabbitMQ/user_reg/models"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"time"
)

func main() {

	r := gin.New()

	r.POST("/user", func(context *gin.Context) {
		user := models.UserModel{}
		err := context.BindJSON(&user)
		if err != nil {
			context.JSON(400, gin.H{"result": "error" + err.Error()})
		} else {
			mq := lib.NewMq()
			uid := time.Now().Unix()
			mq.SetConfirm()
			mq.NotifyReturn()
			//err := mq.SendMessage(lib.ROUTER_KEY_USER_REG, lib.EXCHANGE_USER, fmt.Sprintf("%d", uid))
			err := mq.SendDelayedMessage(lib.ROUTER_KEY_USER_REG, lib.EXCHANGE_USER_DELAYED, fmt.Sprintf("%d", uid), 1000)
			if err != nil {
				fmt.Println(err)
				return
			}
			mq.ListenConfirm()
			context.JSON(200, gin.H{"result": uid})
		}
	})

	c := make(chan error)

	go func() {

		err := r.Run(":8080")
		if err != nil {
			c <- err
		}

	}()

	go func() {
		err := lib.MQInit()
		if err != nil {
			c <- err
		}

		//延迟队列
		err = lib.NewDelayedInitMQ()
		if err != nil {
			c <- err
		}
	}()
	err := <-c
	log.Fatal("退出服务", err)

}
